package company.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import company.common.R;
import company.domain.Feedingpoints;
import company.service.FeedingpointsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class FeedingpointsController {

    @Resource
    FeedingpointsService feedingpointsService;

    /*
     * 分页查询
     * */
    @RequestMapping("/selectAllFeedingpoints/{page}")
    public R selectAllFeedingpoints(@PathVariable("page") int page,@RequestBody Feedingpoints feedingpoints){
        QueryWrapper<Feedingpoints> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(feedingpoints.getLocation()!="" && !("".equals(feedingpoints.getLocation())),"Location",feedingpoints.getLocation());
        //第一个参数表示第几页，第二个参数表示每页的条数
        PageHelper.startPage(page,3);
        List<Feedingpoints> list=this.feedingpointsService.list(queryWrapper);
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return new R().add("pageInfo",pageInfo);
    }

    /*
    * 新增喂养点
    * */
    @PostMapping("/AddFeedingpoints")
    public R AddFeedingpoints(@RequestBody Feedingpoints feedingpoints){
        this.feedingpointsService.save(feedingpoints);
        return new R();
    }

    /*
     * 喂养点删除
     * */
    @RequestMapping("/DelFeedingpoints/{id}")
    public R DelFeedingpoints(@PathVariable("id") int id){
        boolean remove = this.feedingpointsService.removeById(id);
        R r=new R();
        if(remove==true){
            r.setMessage("删除成功");
        }else{
            r.setMessage("删除失败");
        }
        return r;
    }

    /*
     * 根据喂养点id查询
     * */
    @PostMapping("/selectFeedingpoints")
    public R selectFeedingpoints(@RequestBody Feedingpoints feedingpoints){
        Feedingpoints byId = this.feedingpointsService.getById(feedingpoints.getFeedingpointid());
        return new R().add("byId",byId);
    }

    /*
     * 修改喂养点
     * */
    @PostMapping("/uploadFeedingpoints")
    public R uploadFeedingpoints(@RequestBody Feedingpoints feedingpoints){
        this.feedingpointsService.updateById(feedingpoints);
        return new R();
    }

    /*
     * 分页查询
     * */
    @RequestMapping("/selectAllFeedingpoints2/{page}")
    public R selectAllFeedingpoints2(@PathVariable("page") int page){
        //第一个参数表示第几页，第二个参数表示每页的条数
        PageHelper.startPage(page,2);
        List<Feedingpoints> list=this.feedingpointsService.list();
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return new R().add("pageInfo",pageInfo);
    }

}
